<?php

class service_l_product
{
    const MAX = 1000;

    public function __construct(){
        
    }
    public function get_product($prod_id) {
        $product = new product_m_product();
        return $product->get_by_id($prod_id);
    }

    /*      function save_product($array_data)
     *
     *      This method will be used for both Product Add and Edit.
     *      For Adding New Product, $array_data['prod_id'] will be empty.
     *      This method will return Product Object if Product Added or Edited successfully and return proper error message in case of saving data in any of Product Table
     */
    public function save_product($array_data) {
        $return_msg;

        //----------------------------------        Insert record into Table Products       --------------      Start
        $p = new product_m_product();
        if(!string.IsNullOrEmpty($array_data['prod_id'].Trim())) // In case of Edit (Existing Product)
            $p = $p->get_by_id($array_data['prod_id']);

        $p->part_num = $array_data['part_num'];
        $p->upc = $array_data['upc'];
        $p->price = $array_data['price'];
        $p->cost = $array_data['cost'];
        // Begin transaction
        $p->trans_begin();
        $p->save();
        // Check status of transaction
        if ($p->trans_status() === FALSE)
        {
            // Transaction failed, rollback
            $p->trans_rollback();
            $return_msg = 'Failed to insert record into Table Products';
            return $return_msg;
        }
        //----------------------------------        Insert record into Table Products       --------------      End

        //----------------------------------        Insert record into Table languages_products       --------------      Start
        $pt = new text_m_product();
        if(!string.IsNullOrEmpty($array_data['prod_id'].Trim())) // In case of Edit (Existing Product)
            $pt = $pt->get_by_product_id($prod_id);
        $pt->product_id = $p->id;
        $pt->language_id = $array_data['language_id'];
        $pt->short_desc = $array_data['short_desc'];
        $pt->long_desc = $array_data['long_desc'];
        $pt->save();
        if ($pt->trans_status() === FALSE)
        {
            // Transaction failed, rollback
            $p->trans_rollback();
            $return_msg = 'Failed to insert record into Table languages_products';
            return $return_msg;
        }
        //----------------------------------        Insert record into Table languages_products       --------------      End

        //----------------------------------        Insert record into Table categories_products       --------------      Start
        if(string.IsNullOrEmpty($array_data['prod_id'].Trim()))   //For New Product, we need to insert record into Table categories_products
        {
            $pc = new products_m_category();
            $pc->category_id = $array_data['cat_id'];
            $pc->product_id = $p->id;
            $pc->save();
            if ($pc->trans_status() === FALSE)
            {
                $p->trans_rollback();
                $return_msg = 'Failed to insert record into Table categories_products';
                return $return_msg;
            }
        }
        //----------------------------------        Insert record into Table categories_products       --------------      End

        //----------------------------------        Insert record into Table products_tax_exemptions       --------------      Start
        $pte = new tax_exemptions_m_product();
        $pte->product_id = $p->id;
        if(array_key_exists('chkPst',$array_data)&&array_key_exists('chkGst',$array_data))
        {
            $pte->delete_all();
            if ($pte->trans_status() === FALSE)
            {
                $p->trans_rollback();
                $return_msg = 'Failed to delete record into Table products_tax_exemptions';
                return $return_msg;
            }
        }
        else if((array_key_exists('chkPst',$array_data)&&!array_key_exists('chkGst',$array_data))||(!array_key_exists('chkPst',$array_data)&&array_key_exists('chkGst',$array_data)))
        {
            //      Insert/Delete record in Table products_tax_exemptions for PST
            if(array_key_exists('chkPst',$array_data))
            {
                $pte->tax_id = 1;
                $pte->save();
                if ($pte->trans_status() === FALSE)
                {
                    $p->trans_rollback();
                    $return_msg = 'Failed to insert record into Table products_tax_exemptions, Tax ID = 1';
                    return $return_msg;
                }
            }
            else
            {
                $pte->tax_id = 1;
                $pte->delete();
                if ($pte->trans_status() === FALSE)
                {
                    $p->trans_rollback();
                    $return_msg = 'Failed to delete record into Table products_tax_exemptions, Tax ID = 1';
                    return $return_msg;
                }
            }


            //      Insert/Delete record in Table products_tax_exemptions for GST
            if(array_key_exists('chkGst',$array_data))
            {
                $pte->tax_id = 2;
                $pte->save();
                if ($pte->trans_status() === FALSE)
                {
                    $p->trans_rollback();
                    $return_msg = 'Failed to insert record into Table products_tax_exemptions, Tax ID = 2';
                    return $return_msg;
                }
            }
            else
            {
                $pte->tax_id = 2;
                $pte->delete();
                if ($pte->trans_status() === FALSE)
                {
                    $p->trans_rollback();
                    $return_msg = 'Failed to delete record into Table products_tax_exemptions, Tax ID = 2';
                    return $return_msg;
                }
            }
        }
        //----------------------------------        Insert record into Table products_tax_exemptions       --------------      End

        //----------------------------------        Insert record into Table product_extra_field_data       --------------      End
        $pefd = new extra_field_data_m_product();
        $pefd->product_id = $p->id;
        $pefd->field_id = 1;
        $pefd->data = $array_data['selType'];
        $pefd->save();
        if ($pefd->trans_status() === FALSE)
        {
            $p->trans_rollback();
            $return_msg = 'Failed to insert record into Table product_extra_field_data, field_id = 1';
            return $return_msg;
        }

        $pefd->field_id = 2;
        if(array_key_exists('chkHealthyChoice',$array_data))
            $pefd->data = "true";
        else
            $pefd->data = "false";
        $pefd->save();
        if ($pefd->trans_status() === FALSE)
        {
            $p->trans_rollback();
            $return_msg = 'Failed to insert record into Table product_extra_field_data, field_id = 2';
            return $return_msg;
        }
        //----------------------------------        Insert record into Table product_extra_field_data       --------------      End

        // Transaction successful, commit
        $p->trans_commit();
        
        return $p;
    }

    public function get_products($cat_id)
    {
        $objCatService = new service_l_category();
        return  $objCatService->get_products($cat_id);
    }

    private function get_product_object($product_id_or_object){
        $product = get_object($product_id_or_object, "product_m_product");
        return $product;
    }
    public function create_dimension_object($prod_id){
       $objProd_from_dimension = new dimension_m_product();
       $objProd_from_dimension->where("id", $prod_id);
       $objProd_from_dimension->get();
       $objProd_from_dimension->convert_to_simple_data();
       return $objProd_from_dimension;
    }

    public function create_prod_lang_object($prod_id){
       $objProd_from_languages = new text_m_product();
       $objProd_from_languages->where("id", $prod_id);
       $objProd_from_languages->get();
       return $objProd_from_languages->convert_to_simple_data();
    }

    public function create_prod_image_object($prod_id){
       $objProd_images = new image_m_product();
       $objProd_images->where("product_id", $prod_id);
       $objProd_images->get();
       return $objProd_images;
    }
    // <editor-fold defaultstate="collapsed" desc="PRODUCT CUSTOM FIELD FUNCTIONS">

    public function get_extra_field_data($product_id_or_object,$index_by='name',$language_id=NULL){
        $product = $this->get_product_object($product_id_or_object);
        
        if($language_id===NULL){
            $language_id = service_l_core::get_current_site()->language_id;
        }
        if($product!==NULL){
            $product_id = $product->id;
            $sql = "SELECT pefdef.id ,pefd.field_id,language_id,product_id,label,name,data FROM product_extra_field_data pefd
                    INNER JOIN    product_extra_field_definitions pefdef on pefdef.id=pefd.field_id
                    INNER JOIN    product_extra_field_languages pefl ON pefl.field_id=pefdef.id
                    WHERE language_id='$language_id' AND product_id='$product_id'";
            $query = get_instance()->db->query($sql);
            $return = array();
            if($query->num_rows()>0){
                foreach ($query->result() as $row) {
                    $return[$row->$index_by] = new extra_data_m_product($row);
                }                
            }
            return $return;
        }else{
            return NULL;
        }
    }
    
    //</editor-fold>

}
?>